package com.servlet;

import com.pojo.*;
import com.service.ArticleService;
import com.service.CommentService;
import com.service.ManagerService;
import com.service.SearchService;
import com.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/articleServlet")
public class ArticleServlet extends BaseServlet{

    /**
     *  发布博客
     */
    protected void publish_blog(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
          String blog_id=ArticleService.publish_blog(req);
           resp.getWriter().write(ResponseUtil.SUCCESS("发布成功",blog_id));
    }

    /**
     * 保存草稿
     */
    protected void save_draft(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              ArticleService.save_draft(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("保存草稿成功",null));
    }

    /**
     * 编辑博客
     */
    protected void editor_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
          ArticleService.editor_blog(req);
          resp.getWriter().write(ResponseUtil.SUCCESS("编辑成功",null));
    }

    /**
     * 二次保存草稿
     */
    protected void save_draft_again(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
          ArticleService.editor_blog(req);
          resp.getWriter().write(ResponseUtil.SUCCESS("保存草稿成功",null));
    }

    /**
     * 获取所有专栏
     */
    protected void get_column_list(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        List<String> column_list=ArticleService.get_column_list(req);
        resp.getWriter().write(ResponseUtil.SUCCESS("成功获取专栏",column_list));
    }

    /**
     * 新建专栏
     */
    protected void add_column(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
           ArticleService.add_column(req);
            resp.getWriter().write(ResponseUtil.SUCCESS("新建专栏成功",null));
    }

    /**
     * 自定义标签
     */
    protected void add_label(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
            ArticleService.add_label(req);
            resp.getWriter().write(ResponseUtil.SUCCESS("自定义标签成功",null));
    }

    /**
     * 查找标签
     */
    protected void search_label(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
          List<String> label_list=ArticleService.search_label(req);
          resp.getWriter().write(ResponseUtil.SUCCESS("返回查找到的标签",label_list));
    }

    /**
     * 获取博客信息
     */
    protected void get_blog_info(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        System.out.println("[info]获取博客信息");
        Article article =ArticleService.get_blog_content(req);
        resp.getWriter().write(ResponseUtil.SUCCESS("获取博客信息",article));
    }

    /**
     * 获取当前文章专栏
     */
    protected void get_blog_column(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
           List<String>column_list=ArticleService.get_blog_column(req);
           resp.getWriter().write(ResponseUtil.SUCCESS("获取当前文章专栏",column_list));
    }
    /**
     * 获取当前文章标签
     */
    protected void get_blog_label(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        List<String>label_list=ArticleService.get_blog_label(req);
        resp.getWriter().write(ResponseUtil.SUCCESS("获取当前文章标签",label_list));
    }

    /**
     * 修改专栏名
     */
    protected void change_column_name(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
             ArticleService.change_column_name(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("修改专栏成功",null));
    }

    /**
     * 专栏查重
     */
    protected void check_column(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              if(ArticleService.check_column(req)){
                 resp.getWriter().write(ResponseUtil.SUCCESS("专栏没重复",null));
              }else{
                  resp.getWriter().write(ResponseUtil.FAIL("专栏重复",null));
              }
    }

    /**
     * 删除专栏
     */
    protected void del_column(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
             ArticleService.del_column(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("已删除专栏",null));
    }

    /**
     * 获取专栏中的博客
     */
     protected void get_column_article(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
                List<Article> articles=ArticleService.get_column_article(req);
                resp.getWriter().write(ResponseUtil.SUCCESS("获取专栏中的博客",articles));
     }

    /**
     * 移除专栏中的文章
     */
    protected void del_column_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
                ArticleService.del_column_blog(req);
                resp.getWriter().write(ResponseUtil.SUCCESS("移除专栏中的博客",null));
    }

    /**
     * 获取内容管理中的所有博客
     */
    protected void get_content_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
             List<Article> articles=ArticleService.get_all_blog(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("获取所有博客",articles));
    }

    /**
     * 删除博客
     */
    protected void del_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
            ArticleService.del_blog(req);
            resp.getWriter().write(ResponseUtil.SUCCESS("成功删除博客",null));
    }

    /**
     * 获得近期博客
     */
    protected void get_recent_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
            List<Article>articles=ArticleService.get_recent_article(req);
            resp.getWriter().write(ResponseUtil.SUCCESS("获取近期文章",articles));
    }

    /**
     * 搜素博客
     */
    protected void search_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
           List<Article>articles= SearchService.search_blog(req);
           resp.getWriter().write(ResponseUtil.SUCCESS("返回搜素到的博客",articles));
    }
    /**
     * 搜索专栏
     */
    protected void search_column(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
         List<Column>columns=SearchService.search_column(req);
         resp.getWriter().write(ResponseUtil.SUCCESS("返回搜索到的专栏",columns));
    }

    /**
     * 获取专栏作者信息
     */
    protected void get_column_author(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              Author author=ArticleService.get_column_author(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("获取到专栏的作者信息",author));
    }

    /**
     * 获取专栏详情中的博客
     */
     protected void get_column_info_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
             List<Article> articles=ArticleService.get_column_info_blog(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("获取到专栏中的博客",articles));
     }

    /**
     * 获取作者的专栏
     */
    protected void get_author_column(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
            List<Column>columns=ArticleService.get_author_column(req);
            resp.getWriter().write(ResponseUtil.SUCCESS("获取作者的专栏",columns));
    }

    /**
     * 点赞博客
     */
    protected void support_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              ArticleService.support_blog(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("点赞成功",null));
    }

    /**
     * 取消点赞博客
     */
    protected void cancel_support_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              ArticleService.cancel_support_blog(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("已取消点赞该博客",null));
    }

    /**
     * 收藏博客
     */
    protected void collection_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
             ArticleService.collection_blog(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("已收藏该博客",null));
    }

    /**
     * 取消收藏博客
     */
    protected void cancel_collection_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
             ArticleService.cancel_collection_blog(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("已取消收藏博客",null));
    }

    /**
     * 获取用户是否收藏文章
     */
    protected void get_author_is_collection(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        Operation operation=ArticleService.get_user_operation(req);
        if(operation!=null)
        {resp.getWriter().write(ResponseUtil.SUCCESS("获取到用户对文章的操作",operation));}
        else{
             resp.getWriter().write(ResponseUtil.FAIL("没有点赞该博客",null));
        }
    }

    /**
     * 获取用户是否点赞文章
     */
    protected void get_author_is_support(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        Operation operation=ArticleService.get_user_is_support(req);
        resp.getWriter().write(ResponseUtil.SUCCESS("获取到用户对文章的操作",operation));
    }

    /**
     * 获取收藏列表
     */
    protected void get_collection_list(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              List<Article> articles=ArticleService.get_collection_list(req);
              resp.getWriter().write(ResponseUtil.SUCCESS("获取收藏列表",articles));
    }

    /**
     * 获取草稿列表
     */
    protected void get_draft_list(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
                 List<Article>articles=ArticleService.get_draft_list(req);
                 resp.getWriter().write(ResponseUtil.SUCCESS("获取草稿列表",articles));
    }

    /**
     * 发表评论
     */
    protected void write_comment(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
          Comment comment=CommentService.write_comment(req);
          resp.getWriter().write(ResponseUtil.SUCCESS("发表评论",comment));
    }

    /**
     * 获取文章评论
     */
    protected void get_article_comment(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
             List<Comment> comments=CommentService.get_article_comment(req);
             resp.getWriter().write(ResponseUtil.SUCCESS("获取文章评论",comments));
    }

    /**
     * 获取热门推荐
     */
    protected void get_hot_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              List<Article> articles=ArticleService.get_hot_blog();
              resp.getWriter().write(ResponseUtil.SUCCESS("获取热门推荐",articles));
    }

    /**
     * 获取最新文章
     */
    protected void get_latest_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
              List<Article> articles=ArticleService.get_latest_blog();
              resp.getWriter().write(ResponseUtil.SUCCESS("获取最新博文",articles));
    }

    /**
     * 获取未审核博客
     */
    protected void get_not_check_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
            String currentPageStr=req.getParameter("currentPage");
            int currentPage=0;

            if(currentPageStr.length()>0)
            {
                currentPage=Integer.parseInt(currentPageStr);
            }else{
                currentPage=1;
            }

             PageBean<Article> pb=ArticleService.get_not_check_blog(currentPage);

            resp.getWriter().write(ResponseUtil.SUCCESS("获取未审核文章",pb));
    }

    /**
     * 获取管理的文章
     */
     protected void get_manage_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
         String currentPageStr=req.getParameter("currentPage");
         int currentPage=0;

         if(currentPageStr.length()>0)
         {
             currentPage=Integer.parseInt(currentPageStr);
         }else{
             currentPage=1;
         }

         PageBean<Article> pb=ArticleService.get_manage_blog(currentPage);

         resp.getWriter().write(ResponseUtil.SUCCESS("获取管理文章",pb));
     }


    /**
     * 通过博客
     */
    protected void pass_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        ManagerService.pass_blog(req);
        resp.getWriter().write(ResponseUtil.SUCCESS("审核通过",null));
    }

    /**
     * 不通过博客
     */
    protected void not_pass_blog(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
           ManagerService.not_pass_blog(req);
           resp.getWriter().write(ResponseUtil.SUCCESS("审核不通过",null));
    }
}

